/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor;

import java.util.HashMap;

import com.acarter.propertytable.PropertyTable;

/**
 * @author Carter
 *
 */
public abstract class A_PropertyPageHandler {

	/** Set of property pages for each tree object */
	protected HashMap<Class<?>, A_PropertyPage> pages = new HashMap<Class<?>, A_PropertyPage>();
	
	/**
	 * 
	 */
	public A_PropertyPageHandler(PropertyTable table) {
		
		registerPropertyPage(null, new EmptyPropertyPage());
		
		initPropertyPages(table);
	}
	
	/**
	 * Subclasses must load their own set of default property pages.
	 */
	protected abstract void initPropertyPages(PropertyTable table);
	
	/**
	 * Returns the property page that is associated with the object. If an exact match is not defined, then 
	 * it will search up the inheritance of the object to find the next closest match.
	 * 
	 * @param object The object who's property page you want returned.
	 * @return The property page matching the object type.
	 */
	public A_PropertyPage getPropertyPage(Object object) {

		A_PropertyPage page = null;
		
		if(object != null) {

			Class<?> c = object.getClass();
			
			page = pages.get(c);
		
			if(page == null) {
			
				while((c = c.getSuperclass()) != null) {

					if((page = pages.get(c)) != null)
						break;
				}
			}
		}
		
		if(page == null)
			page = pages.get(null);
		
		return page;
	}
	
	/**
	 * Adds the property page/object type pair to the collection.
	 * 
	 * @param classType
	 * @param page
	 */
	public void registerPropertyPage(Class<?> classType, A_PropertyPage page) {
		
		pages.put(classType, page);
	}
}
